package mortal.game.control
{
    import com.gengine.global.*;
    import com.gengine.resource.*;
    import com.gengine.resource.info.*;
    import flash.display.*;
    import flash.events.*;
    import mortal.common.sound.*;
    import mortal.game.mvc.*;
    import mortal.mvc.core.*;

    public class EffectController extends Controller
    {
        private var _movieEffct:MovieClip;
        private var _count:int;

        public function EffectController()
        {
            
        }

        private function onTaskGetHandler(param1:Object) : void
        {
            this.loadEffect("gettask.swf");
            SoundManager.instance.soundPlay(SoundTypeConst.AcceptTask);
            
        }

        private function onTaskEndHandler(param1:Object) : void
        {
            this.loadEffect("endtask.swf");
            SoundManager.instance.soundPlay(SoundTypeConst.CompleteTask);
            
        }

        private function loadEffect(param1:String) : void
        {
            LoaderManager.instance.load(param1, this.onEffectLoaded);
            
        }

        private function onEffectLoaded(param1:SWFInfo) : void
        {
            if (this._movieEffct && this._movieEffct.parent)
            {
                this._movieEffct.removeEventListener(Event.ENTER_FRAME, this.onMovieEffectEnterFrame);
                this._movieEffct.parent.removeChild(this._movieEffct);
            }
            this._movieEffct = param1.clip;
            this._movieEffct.scaleX = 1.5;
            this._movieEffct.scaleY = 1.5;
            this._movieEffct.mouseEnabled = false;
            this._movieEffct.mouseChildren = false;
            Global.stage.addChild(this._movieEffct);
            this._movieEffct.addEventListener(Event.ENTER_FRAME, this.onMovieEffectEnterFrame);
            this.stageResize();
            this._movieEffct.gotoAndPlay(1);
            
        }

        private function onMovieEffectEnterFrame(event:Event) : void
        {
            var _loc_2:String = this;
            var _loc_3:* = this._count + 1;
            _loc_2._count = _loc_3;
            if (this._count % 2 == 0)
            {
                this._movieEffct.nextFrame();
                this._movieEffct.nextFrame();
            }
            if (this._movieEffct.currentFrame == this._movieEffct.totalFrames)
            {
                this._movieEffct.removeEventListener(Event.ENTER_FRAME, this.onMovieEffectEnterFrame);
                if (this._movieEffct.parent)
                {
                    this._movieEffct.parent.removeChild(this._movieEffct);
                }
                this._count = 0;
            }
            
        }

        public function stageResize() : void
        {
            if (this._movieEffct && this._movieEffct.parent)
            {
                this._movieEffct.x = (Global.stage.stageWidth - 290 * 1.5) / 2;
                this._movieEffct.y = 30;
            }
            
        }

        override protected function initServer() : void
        {
            super.initServer();
            NetDispatcher.addCmdListener(ServerCommand.TaskGetRes, this.onTaskGetHandler);
            NetDispatcher.addCmdListener(ServerCommand.TaskEndRes, this.onTaskEndHandler);
            
        }

    }
}
